import {
	ref,
	relative,
	nextTick
} from 'vue';
import {
	request
} from "../network/request";
import {
	ElMessage,
	ElMessageBox
} from 'element-plus'

export default function() {
	// name: "DataList",

	const mixinViewModuleOptions = ref({
		activatedIsNeed: true, // 此页面是否在激活（进入）时，调用查询数据列表接口？
		getDataListURL: '', // 数据列表接口，API地址
		getDataListIsPage: false, // 数据列表接口，是否需要分页？
		deleteURL: '', // 删除接口，API地址
		deleteIsBatch: false, // 删除接口，是否需要批量？
		deleteIsBatchKey: 'id', // 删除接口，批量状态下由那个key进行标记操作？比如：pid，uid...
		exportURL: '' // 导出接口，API地址
	});

	// 默认属性
	const dataForm = ref({}) // 查询条件
	const dataList = ref([]); // 数据列表
	const order = ref('') // 排序，asc／desc
	const orderField = ref('') // 排序，字段
	const page = ref(1) // 当前页码
	const limit = ref(10) // 每页数
	const total = ref(0) // 总条数
	const dataListLoading = ref(false) // 数据列表，loading状态
	const dataListSelections = ref([]) // 数据列表，多选项
	const addOrUpdateVisible = ref(false) // 新增／更新，弹窗visible状态
	const addOrUpdate = ref(null)

	const getDataList = () => {
		dataListLoading.value = false;

		request({
			url: mixinViewModuleOptions.value.getDataListURL,
			method: 'get',
			params: _.merge({
				order: order.value,
				orderField: orderField.value,
				page: mixinViewModuleOptions.value.getDataListIsPage ? page.value : null,
				limit: mixinViewModuleOptions.value.getDataListIsPage ? limit.value : null
			}, dataForm.value)

		}).then(function(res) {

			dataListLoading.value = false;
			if (res.code !== 0) {
				dataList.value = [];
				total.value = 0;
				return ElMessage.error(res.msg);
			}
			dataList.value = mixinViewModuleOptions.value.getDataListIsPage ? res.data.list : res.data;
			if (res.data.param) {
				dataForm.value = res.data.param;
			}
			total.value = mixinViewModuleOptions.value.getDataListIsPage ? res.data.total : 0;
		}).catch(function() {
			dataListLoading.value = false;
		})
	}
	// 多选
	const dataListSelectionChangeHandle = (val) => {
		dataListSelections.value = val;
	}
	// 排序
	const dataListSortChangeHandle = (data) => {
		if (!data.order || !data.prop) {
			order.value = '';
			orderField.value = '';
			return false;
		}
		order.value = data.order.replace(/ending$/, '');
		orderField.value = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase();
		getDataList();
	}
	// 分页, 每页条数
	const pageSizeChangeHandle = (val) => {
		page.value = 1;
		limit.value = val;
		getDataList();
	}
	// 分页, 当前页
	const pageCurrentChangeHandle = (val) => {
		page.value = val;
		getDataList();
	}
	// 新增 / 修改
	const addOrUpdateHandle = (id) => {
		//
		addOrUpdateVisible.value = true;
		nextTick(function() {
			addOrUpdate.value.dataForm.id = id;
			addOrUpdate.value.init();
		})
	}
	// 删除
	const deleteHandle = (id) => {
		if (mixinViewModuleOptions.value.deleteIsBatch && !id && dataListSelections.length <= 0) {
			return ElMessage({
				message: '请选择删除项',
				type: 'warning',
				duration: 1000
			});
		}
		ElMessageBox.confirm('确定进行删除操作?', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(function() {
			let url = mixinViewModuleOptions.value.deleteURL + (mixinViewModuleOptions.value.deleteIsBatch ?
				'' : '/' + id);
			let para = mixinViewModuleOptions.value.deleteIsBatch ? (
				id ? [id] : dataListSelections.value.map(function(item) {
					return item[mixinViewModuleOptions.value.deleteIsBatchKey];
				})
			) : {};

			request({
				url: url,
				method: 'delete',
				data: para
			}).then(function(res) {
				if (res.code !== 0) {

					return ElMessage.error(res.msg);
				}
				ElMessage({
					message: '操作成功',
					type: 'success',
					duration: 500,
					onClose: function() {
						getDataList();
					}
				});
			}).catch(function() {});

		}).catch(function() {});
	}

	// 导出
	const exportHandle = () => {
		request({
			url: mixinViewModuleOptions.value.exportURL,
			method: 'get',
			params: dataForm.value,
			responseType: 'blob'
		}).then((res) => {
			let fileName = 'SamplePointsDetail.xls' //excel文件名称
			let blob = new Blob([res.data], {
				type: 'application/x-xls'
			}) //word文档为msword,pdf文档为pdf，excel文档为x-xls
			if (window.navigator.msSaveOrOpenBlob) {
				navigator.msSaveBlob(blob, fileName);
			} else {
				var link = document.createElement('a');
				link.href = window.URL.createObjectURL(blob);
				link.download = fileName;
				link.click();
				window.URL.revokeObjectURL(link.href);
			}

		})


	}


	return {
		order,
		orderField,
		dataForm,
		mixinViewModuleOptions,
		getDataList,
		dataList,
		total,
		page,
		limit,
		dataListLoading,
		dataListSelectionChangeHandle,
		dataListSortChangeHandle,
		pageSizeChangeHandle,
		pageCurrentChangeHandle,
		addOrUpdateHandle,
		deleteHandle,
		exportHandle,
		addOrUpdateVisible,
		dataListSelections,
		addOrUpdate
	}
}
